package DSENS;


import java.util.ArrayList;


public class Node {

    private static int nodeidentifier=-1;
    final int ID;
    enum NodeState{FAILURE, OFF, ON, WORKING}
    private NodeState state;
    public ArrayList<Neighbour> neighborood; //Insieme delle connessioni col nodo in questione

    //Recupero dello stato del nodo
    public NodeState getState() {
        return state;
    }

    public void setState(NodeState state) {
        this.state = state;
    }

    public int getNodeidentifier() {
        return ID;
    }

    public ArrayList<Neighbour> getNeighborood() {
        return neighborood;
    }

    public void setNeighborood(ArrayList<Neighbour> neighborood) {
        this.neighborood = neighborood;
    }
    public void addNeighbour(Neighbour nb){
        this.neighborood.add(nb);
    }

    public Neighbour getNeighbour(int neighbourID){
        return this.neighborood.get(neighbourID);
    }

    public int getNeighbourIndex(Neighbour n){
        return this.neighborood.indexOf(n);
    }

    public void removeNeighbour(int neighbourID){
        this.neighborood.remove(neighbourID);
    }

    public void orderNeighbour_LinkSize(){
        Neighbour temp;
        int itemp=0;
        if (this.neighborood.size()!=0)
            itemp=this.neighborood.get(0).getLinkSize();
        //for(int i=0; i<this.neighborood.size();i++) System.out.println(i+"-"+this.neighborood.get(i).getLinkSize());
        for(int i=0; i<this.neighborood.size();i++)
            for(int j=0; j<this.neighborood.size();j++){
                if(this.neighborood.get(i).getLinkSize()>this.neighborood.get(j).getLinkSize()){
                    temp=this.neighborood.get(i);
                    this.neighborood.set(i, this.neighborood.get(j));
                    this.neighborood.set(j, temp);
                }
          }
          //for(int i=0; i<this.neighborood.size();i++) System.out.println(this.neighborood.get(i).getLinkSize());
    }

    public void receiveEvent(Event event){}

    public void manageEvent(Event event){}

    public void run(Event event) {
        this.receiveEvent(event);
        this.manageEvent(event);
    }

    //Costruttori
    public Node(NodeState state) {
        Node.nodeidentifier++;
        this.ID=Node.nodeidentifier;
        this.setState(state);
        this.neighborood = new ArrayList<Neighbour>(1);
    }
    
    public Node() {
        Node.nodeidentifier++;
        this.ID=Node.nodeidentifier;
        this.state = state.ON;
        this.neighborood = new ArrayList<Neighbour>(1);
    }

    public Node(ArrayList<Neighbour> neighborood) {
        Node.nodeidentifier++;
        this.ID=Node.nodeidentifier;
        this.state = state.ON;
        this.neighborood = neighborood;
}
    public void printNode(){
        System.out.println("Node#"+this.getNodeidentifier()+" state: "+this.state);
        for(int i=0;i<this.neighborood.size();i++) System.out.println("\tNB#"+i+" Node#"+ this.neighborood.get(i).getId());
    }


}

